<?php

/**
 * Tells Smit that we are running it un-installed
 */
define('RUN_GENERIC', 'run');

require_once '../init.php';

require_once './InstallUtils' . EXT;

$utils = InstallUtils::getInstance();

$utils->installer = 'Installer';

$utils->set_db_settings('mysql', 'localhost', NULL, 'root', '', 'swixs', 'swixs_');

$intro = '<p><em>Thank you</em> for choosing SmitBoard as your forum solution, the installer will guide you through the procedure of configuring this copy of SmitBoard on your server. If you are upgrading please continue to install and then run the upgrader.</p>';

$reqs = '<p>Before continuing we would like you to know that the Basic requirments for SmitBoard are <em>PHP 5.1.0 or greater</em> and <em>a database</em> to store SmitBoard\'s data in (MySQL, PostgreSQL, SQLite v2 or 3). If you meet these requirements, then please continue and accept the license agreement and then some accurate tests can be ran.</p>';

$explain_reqs = '<p>SmitBoard has checked various Environment settings, anything in <em><span style="color: red">red</span></em> needs to be corrected while <em><span style="color: blue">blue</span></em> means that SmitBoard can run without that feature, lastly <em><span style="color: green">green</span></em> means your Good to go. You only need one database option to be available.</p>';

$explain_db = '<p>It\'s time to enter your database settings in order for SmitBoard to create it\'s tables and insert the default data, if you are unsure what these settings are ask your provider or system administrator.</p>';

switch($utils->base->URI(1, 'act'))
{
	default:
		$utils->box('general', 'Installation', $intro);
		$utils->box('notice', 'Requirements', $reqs);
		$utils->form_open('setup', 'act=license');
		$utils->form_button('Continue');
		$utils->form_close();
		$utils->navigation('SmitBoard ' . SMIT_HUMAN_VERSION);
		$utils->flush();
	break;

	case 'license':
		$utils->box('notice', 'License', '<p>SmitBoard is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. SmitBoard is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.</p><p>By Continuing you agree to the terms &amp; conditions of the GNU General Public License.</p>');
		$utils->box('general', 'GNU General Public License v3', '<p>' . nl2br(htmlentities(file_get_contents('./gpl.txt'))) . '</p>', 'style="overflow: auto; height: 300px;"');
		//$utils->rowset_open();
		$utils->form_open('setup', 'act=reqs');
		$utils->form_button('I Agree, Continue');
		$utils->form_close();
		//$utils->rowset_close();
		$utils->navigation('License');
		$utils->flush('License');
	break;

	case 'reqs':
		$checks = $utils->do_env_checks();

		$utils->box('info', 'Requirements', $explain_reqs);
		$utils->box('general', NULL, '<p>The results obtained from testing the requirements</p>');
		$utils->rowset_open();
		$utils->row('PHP Version', $checks['php'], 'SmitBoard needs PHP 5.1.0 or Greater');
		$utils->row('mbstring', $checks['mbstring'], 'SmitBoard needs mbstring to be available for UTF-8 Support');
		$utils->row('Reflection', $checks['reflection'], 'SmitBoard needs reflection to map applications');
		$utils->row('Cache Directory', $checks['cache'], 'The Cache Directory is needs to be readable &amp; writable!');
		//$utils->rowset_close();
		//$utils->box('general', NULL, '<p>You need generally need <em>only</em> the database you intend to use to be supported to install SmitBoard.</p>' . $checks['db']);
		//$utils->rowset_open();
		/*foreach($utils->list_supported_dbms() as $db)
		{
		$utils->row($db['type'], $db['status']);
		//$utils->row('Database Support', $utils->list_supported_dbms() . '<br />' . $db_status, 'You need the databse you intend to use to be supported.');
		}
		*/
		$utils->row('Supported DB Drivers', implode(', ', $utils->scan_drivers()), 'You need generally need <strong>only</strong> the database you intend to use to be supported to install SmitBoard.');
		$utils->rowset_close();
		$utils->form_open('setup', 'act=db');
		$utils->form_button('Continue');
		$utils->form_close();
		$utils->navigation('Requirements');
		$utils->flush('Requirements');
	break;
	case 'db':
		$rules = array(
			'driver' => array(
				'required' => 1,
				'title' => 'Driver',
				'messages' => array('required' => 'You must choose a DB Driver.'),
			),
			'database' => array(
				'required' => 1,
				'title' => 'Database',
				'messages' => array('required' => 'You must choose a database.'),
			),
		);
		if(!$utils->base->lib->validation($rules))
		{
			$utils->box('info', 'Database Settings', $explain_db);
			//print_r($utils->scan_drivers());
			if(count($utils->base->lib->validation_errors) > 0)
			{
				$utils->box('error', 'Errors', '<p>' . implode('</p><p>', $utils->base->lib->validation_errors) . '</p>');
			}
			$utils->form_open('setup', 'act=db');
			$utils->rowset_open();
			$utils->row('Database Driver', $utils->driver_form(), 'Please choose the driver suitable for your database.');
			$utils->row('Database Server', $utils->input('server', 'localhost'), 'This is mostly always correct');
			$utils->row('Database Username', $utils->input('username', 'root'), 'Please enter your database username');
			$utils->row('Database Password', $utils->input('password', ''), 'Please enter your database password');
			$utils->row('Database Name', $utils->input('database', ''), 'Please enter the name of the database you wish to utilize');
			$utils->row('Table Prefix', $utils->input('prefix', 'smit_'), 'You may prefix your database tables with this');
			$utils->rowset_close();
			$utils->form_button('Continue');
			$utils->form_close();
			$utils->navigation('Database Settings');
			$utils->flush('Database Settings');
		}
		else
		{
			$utils->set_db_settings($utils->base->lib->input_post('driver'), $utils->base->lib->input_post('server'), $utils->base->lib->input_post('username'), $utils->base->lib->input_post('password'), $utils->base->lib->input_post('database'), $utils->base->lib->input_post('prefix'));
			if($utils->db_connect())
			{
				$utils->box('general', 'Connection Established!', '<p>The Installer was able to connection to the database with the settings you provided, please continue to write the configuration file.</p>');
				$utils->form_open('setup', 'act=config');
				$utils->form_button('Continue');
				$utils->form_close();
				$utils->navigation('Connection Established!');
				$utils->flush('Connected!');
			}
			else
			{
				$utils->box('error', 'Connection Failed!', '<p>The Installer was able not able to connect to the database with the settings you provided, please review your settings.</p>');
				$utils->form_open('setup', 'act=config');
				$utils->form_button('Continue');
				$utils->form_close();
				$utils->navigation('Connection Failed!');
				$utils->flush('Unconnected!');
			}
		}
	break;
}
?>